package leetCode.thread.leetCode1114;

import java.util.concurrent.Semaphore;

/**
 * 使用信号量机制实现线程协作
 */
public class Foo2 implements Foo{

    Semaphore second = new Semaphore(0);
    Semaphore third = new Semaphore(0);


    @Override
    public void first(Runnable printFirst) throws InterruptedException {
        printFirst.run();
        second.release();
    }

    @Override
    public void second(Runnable printSecond) throws InterruptedException {
        second.acquire();
        printSecond.run();
        third.release();
    }

    @Override
    public void third(Runnable printThird) throws InterruptedException {
        third.acquire();
        printThird.run();
    }

}
